package com.smartstate.IOC.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartstate.IOC.dto.PageDTO;
import com.smartstate.IOC.entity.CarbonEmissions;
import com.smartstate.IOC.mapper.CarbonMapper;
import com.smartstate.IOC.service.CarbonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service
@Slf4j
public class CarbonServiceImpl extends ServiceImpl<CarbonMapper, CarbonEmissions> implements CarbonService {


    @Autowired
    CarbonMapper carbonMapper;


    @Override
    public Page<CarbonEmissions> queryAllPageByDTO(PageDTO pageDTO) {
        return null;
    }

    @Override
    public Integer getCarbonTotal() {
        List<CarbonEmissions> total = carbonMapper.getCarbonTotal();
        ArrayList<Integer> totals = new ArrayList<>();
        for (CarbonEmissions carbonEmissions : total) {
            Integer energy = carbonEmissions.getEnergyActivities();
            Integer industrialProduction = carbonEmissions.getIndustrialProduction();
            Integer wasteDisposal = carbonEmissions.getWasteDisposal();
            totals.add(energy);
            totals.add(industrialProduction);
            totals.add(wasteDisposal);
        }
        Integer integer = totals.stream().reduce(Integer::sum).orElse(0);
        return integer;
    }

    @Override
    public Map<String, Integer> getEmissionsTotal() {
        HashMap<String, Integer> stringListHashMap = new HashMap<>();
//        ArrayList<Integer> total = new ArrayList<>();
        //能源活动碳生产总量
        Integer energyActivitiesTotal = carbonMapper.getEnergyActivitiesTotal();
//        total.add(energyActivitiesTotal);
        stringListHashMap.put("能源活动", energyActivitiesTotal);
        //工业生产碳生产总量
        Integer industrialProduction = carbonMapper.getIndustrialProduction();
//        total.add(industrialProduction);
        stringListHashMap.put("工业生产", industrialProduction);
        //废弃物处理生产碳排放总量
        Integer wasteDisposal = carbonMapper.getWasteDisposal();
//        total.add(wasteDisposal);
        stringListHashMap.put("废弃物处理", wasteDisposal);
        return stringListHashMap;
    }


    /**
     * 当前月份的前六个月碳排放总量
     */
    @Override
    public List<Map<String, Integer>> getMouthSix() {
        ArrayList<Map<String, Integer>> maps = new ArrayList<>();
        //获取当前年月
        Calendar calendar = Calendar.getInstance();
        //月份
        int month = calendar.get(Calendar.MONTH) + 1;
        String toMonth = String.valueOf(month);
        //年份
        int year = calendar.get(Calendar.YEAR);
        String toYear = String.valueOf(year);
        String date = String.join("-", toYear, toMonth);
        //要获取的六个月份
        List<String> sixMonth = getSixMonth(date);
        //六个月不需要包含当前月
        sixMonth.remove(date);
        for (String s : sixMonth) {
            Integer energyActivitiesMouthTotal = carbonMapper.getEnergyActivitiesMouthTotal(s);
            if (energyActivitiesMouthTotal == null) {
                energyActivitiesMouthTotal = 0;
            }
            Integer industrialProductionMouthTotal = carbonMapper.getIndustrialProductionMouthTotal(s);
            if (industrialProductionMouthTotal == null) {
                industrialProductionMouthTotal = 0;
            }
            Integer wasteDisposalMouthTotal = carbonMapper.getWasteDisposalMouthTotal(s);
            if (wasteDisposalMouthTotal == null) {
                wasteDisposalMouthTotal = 0;
            }
            HashMap<String, Integer> sMap = new HashMap<>();
            sMap.put(s, energyActivitiesMouthTotal + industrialProductionMouthTotal + wasteDisposalMouthTotal);
            maps.add(sMap);
        }
        return maps;
    }


    /**
     * * 获取某一月份的前六个月
     * * @param date 日期,格式:"2018-10"
     * * @return
     */
    public List<String> getSixMonth(String date) {
        //返回值
        List<String> list = new ArrayList<String>();
        int month = Integer.parseInt(date.substring(5, 7));
        int year = Integer.parseInt(date.substring(0, 4));
        for (int i = 6; i >= 0; i--) {
            if (month > 7) {
                if (month - i >= 10) {
                    list.add(year + "-" + String.valueOf(month - i));
                } else {
                    list.add(year + "-0" + String.valueOf(month - i));
                }
            } else {
                if (month - i <= 0) {
                    if (month - i + 12 >= 10) {
                        list.add(String.valueOf(year - 1) + "-" + String.valueOf(month - i + 12));
                    } else {
                        list.add(String.valueOf(year - 1) + "-0" + String.valueOf(month - i + 12));
                    }
                } else {
                    if (month - i >= 10) {
                        list.add(String.valueOf(year) + "-" + String.valueOf(month - i));
                    } else {
                        list.add(String.valueOf(year) + "-0" + String.valueOf(month - i));
                    }
                }
            }
        }
        return list;

    }
}
